/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.view;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.LookAndFeel;
import javax.swing.UIManager;

import com.googlecode.duplicatedetector.util.Logger;

/**
 * All available look and feels.
 * 
 * @author Marco Biscaro
 */
public final class LookAndFeels {

	private static final String[] POSSIBLE_LAF = {
			"javax.swing.plaf.metal.MetalLookAndFeel",
			"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" };
	private static final Logger LOGGER = new Logger(LookAndFeels.class);
	public static final Map<String, LookAndFeel> LOOK_AND_FEELS = new HashMap<String, LookAndFeel>();

	static {
		for (String laf : POSSIBLE_LAF) {
			LookAndFeel lookAndFeel = getLaF(laf);
			if (lookAndFeel == null) {
				continue;
			}
			String lafKey = laf.substring(laf.lastIndexOf('.') + 1).replace(
					"LookAndFeel", "");
			LOOK_AND_FEELS.put(lafKey, lookAndFeel);
			LOGGER.debug("Loaded look and feel " + laf);
		}
		LOOK_AND_FEELS.put("System", getLaF(UIManager
				.getSystemLookAndFeelClassName()));
	}

	private LookAndFeels() {
		// can not instantiate
	}

	private static LookAndFeel getLaF(String className) {
		try {
			Class<?> clazz = Class.forName(className);
			return (LookAndFeel) clazz.newInstance();
		} catch (ClassNotFoundException e) {
			LOGGER.warning("Could not load look and feel: " + className);
		} catch (InstantiationException e) {
			LOGGER.warning("Could not load look and feel: " + className);
		} catch (IllegalAccessException e) {
			LOGGER.warning("Could not load look and feel: " + className);
		}
		return null;
	}

	public static String getName(LookAndFeel lookAndFeel) {
		for (Entry<String, LookAndFeel> entry : LOOK_AND_FEELS.entrySet()) {
			if (entry.getValue().equals(lookAndFeel)) {
				return entry.getKey();
			}
		}
		return "Unknown";
	}
}
